﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.WindowsAzure.ServiceRuntime;
using Microsoft.WindowsAzure.Diagnostics;

namespace iuPlace.Web
{
    public class WebRole : RoleEntryPoint
    {
        public override bool OnStart()
        {
            var config = DiagnosticMonitor.GetDefaultInitialConfiguration();

            //PerformanceCounters
            config.PerformanceCounters.DataSources.Add(new PerformanceCounterConfiguration() { CounterSpecifier = @"\Processor(_Total)\% Processor Time", SampleRate = TimeSpan.FromSeconds(1) });
            config.PerformanceCounters.DataSources.Add(new PerformanceCounterConfiguration() { CounterSpecifier = @"\Memory\Available Mbytes", SampleRate = TimeSpan.FromSeconds(1) });
            config.PerformanceCounters.DataSources.Add(new PerformanceCounterConfiguration() { CounterSpecifier = @"\ASP.NET Applications(__Total__)\Requests/Sec", SampleRate = TimeSpan.FromSeconds(10) });
            config.PerformanceCounters.DataSources.Add(new PerformanceCounterConfiguration() { CounterSpecifier = @"\TCPv4\Connections Established", SampleRate = TimeSpan.FromSeconds(10) });
            config.PerformanceCounters.DataSources.Add(new PerformanceCounterConfiguration() { CounterSpecifier = @"\Network Interface(*)\Bytes Received/sec", SampleRate = TimeSpan.FromSeconds(10) });
            config.PerformanceCounters.DataSources.Add(new PerformanceCounterConfiguration() { CounterSpecifier = @"\Network Interface(*)\Bytes Sent/sec", SampleRate = TimeSpan.FromSeconds(10) });
            config.PerformanceCounters.ScheduledTransferPeriod = TimeSpan.FromSeconds(60);

            //Diagnostics
            config.DiagnosticInfrastructureLogs.ScheduledTransferLogLevelFilter = LogLevel.Error;
            config.DiagnosticInfrastructureLogs.ScheduledTransferPeriod = TimeSpan.FromMinutes(5);

            //System
            config.WindowsEventLog.DataSources.Add("System!*");
            config.WindowsEventLog.DataSources.Add("Application!*");
            config.WindowsEventLog.ScheduledTransferLogLevelFilter = LogLevel.Error;
            config.WindowsEventLog.ScheduledTransferPeriod = TimeSpan.FromMinutes(5);

            //Capture complete crash dumps
            Microsoft.WindowsAzure.Diagnostics.CrashDumps.EnableCollection(false);

            //Directories
            config.Directories.ScheduledTransferPeriod = TimeSpan.FromMinutes(5);

            //Logs
            config.Logs.ScheduledTransferLogLevelFilter = LogLevel.Verbose;
            config.Logs.ScheduledTransferPeriod = TimeSpan.FromMinutes(5);

            // Start the diagnostic monitor with the modified configuration.
            DiagnosticMonitor.Start("Microsoft.WindowsAzure.Plugins.Diagnostics.ConnectionString", config);

            return base.OnStart();
        }
    }
}